
/*
HappyPins_2.pde - timers act directly on pins --first example with fixed speeds. Three LED's blinking together at different rates. Three timer channels are used. The channels are numbered 0, 1, and 2.

Copyright (c) 2010 Ed Bennett.  All rights reserved.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/


// Timer commands usually take a channel number and/or a 
// time-value. 
//
// The unit of time is is called a blink. The duration 
// of a blink is related to an AVR jiffy. See
// http://en.wikipedia.org/wiki/Jiffy_%28time%29
//
// This example uses direct control of the pins from the timer channels.
// This method uses less processor power than callbacks or flag polling.
// (see the other examples)


#include <TimerScheduler.h>

// hardware pins
int LED0 = 13;
int LED1 = 12;
int LED2 = 11;
int LED3 = 10;
int LED4 = 9;

//int LED[5]= {13,12,11,10,9};

/*
int TimeHigh0 = 0;
 int TimeHigh0 = 0;
 int TimeHigh0 = 0;int TimeHigh0 = 0;int TimeHigh0 = 0;
 */
/*
int TimeHigh0 = 0;
 int TimeLow0 = 0;
 
 int TimeHigh1 = 0;
 int TimeLow1 = 0;
 
 int TimeHigh2 = 0;
 int TimeLow2 = 0;
 
 int TimeHigh3 = 0;
 int TimeLow3 = 0;
 
 int TimeHigh4 = 0;
 int TimeLow4 = 0;
 */


void setup()
{   
  pinMode(LED0, OUTPUT); 
  pinMode(LED1, OUTPUT); 
  pinMode(LED2, OUTPUT);
  pinMode(LED3, OUTPUT); 
  pinMode(LED4, OUTPUT);  
  /*
  int analogPin0 = 0;
   int analogPin1 = 1;
   int analogPin2 = 2;
   int analogPin3 = 3;
   int analogPin4 = 4;
   int onTime = 5;
   */


  Timer.begin();        // start the hardware timer

  // These are pin-direct from here.

  // attach LED pins to timer channels
  Timer.attachPin(0, LED0); // (channel, value)
  Timer.attachPin(1, LED1);
  Timer.attachPin(2, LED2); 
  Timer.attachPin(3, LED3);
  Timer.attachPin(4, LED4);


  /*
  // using pin-direct mode, you need to set the pin-high
   // and pin-low times seperately
   Timer.setHighTime(0, TimeHigh0); // (channel, value)
   Timer.setLowTime(0, TimeLow0); 
   
   Timer.setHighTime(1,TimeHigh1);
   Timer.setLowTime(1, TimeLow1);
   
   Timer.setHighTime(2, TimeHigh2);
   Timer.setLowTime(2, TimeLow2);
   
   Timer.setHighTime(3, TimeHigh2);
   Timer.setLowTime(3, TimeLow2);
   
   Timer.setHighTime(4, TimeHigh2);
   Timer.setLowTime(4, TimeLow2);
   */

  Timer.start(0);       // (channel)
  Timer.start(1); 
  Timer.start(2); 
  Timer.start(3);       // (channel)
  Timer.start(4); 
  Timer.start(5);

  // END of pin-direct timers

  // beginning of polled timers

  //Timer.repeat(3,1000);

  //Timer.start(3);

  // end of polled timers

}




// poll some flags to change the pin-direct timers
void loop()
{
  unsigned int anaVal;
  for(byte i=0; i<5; i++){
    anaVal=(unsigned) analogRead(5)/4;
    Timer.setHighTime(i, anaVal); // high time is the same for all
  } 

  for(byte i=0; i<5; i++){
    anaVal=  (unsigned) analogRead(i);
    Timer.setLowTime(i,anaVal);
  }


}

